//
//  TTHTTPTool.m
//  WinesMall
//
//  Created by Isaac on 16/8/27.
//  Copyright © 2016年 Isaac. All rights reserved.
//

#import "TTHTTPTool.h"
#import <AFNetworking/AFNetworking.h>

@implementation TTHTTPTool

+(void)get:(NSString *)url params:(NSMutableDictionary *)params isNeedToken:(BOOL)isNeedToken complete:(void (^)(BOOL, id, NSError *))complete{
    
    NSMutableDictionary *paramt = [NSMutableDictionary dictionaryWithDictionary:params];
    if (isNeedToken) {
        [paramt setObject:userToken forKey:@"token"];
    }

    NSLog(@"get:url:%@---param:%@",url, paramt);
//    [paramt setObject:[UserdefaultInfoModel getToken] forKey:@"token"];
    
//    AFNetworkReachabilityManager *reach = [AFNetworkReachabilityManager sharedManager];
//    if (reach.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || reach.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
//        
//        if (complete) complete(NO, nil, nil);
//        
//    }else{
        AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
        ((AFJSONResponseSerializer *)session.responseSerializer).removesKeysWithNullValues = YES;///重要店
        session.requestSerializer.timeoutInterval = 10;
        session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

        [session GET:url parameters:paramt progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"get:   respj:   %@  msg:  %@",responseObject, responseObject[@"msg"]);
            if (complete) complete(YES, responseObject, nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error:   %@",error);
            if (complete) complete(YES, nil, error);
            
        }];
        
//    }
    
}

+ (void)post:(NSString *)url params:(NSMutableDictionary *)params isNeedToken:(BOOL)isNeedToken complete:(void (^)(BOOL, id, NSError *))complete{
    
    /*监听网络状态，当状态为没网时返回nil*/
    NSMutableDictionary *paramt = [NSMutableDictionary dictionaryWithDictionary:params];
    if (isNeedToken) {
        [paramt setObject:userToken forKey:@"token"];
    }
    NSLog(@"url:%@---param:%@",url, paramt);

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    ((AFJSONResponseSerializer *)session.responseSerializer).removesKeysWithNullValues = YES;///重要店
    session.requestSerializer.timeoutInterval = 10;
    
    
    if ([WSMTools wsmTool].isPost) {
        session.responseSerializer = [AFHTTPResponseSerializer serializer];
    }else {
        session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    }
        [session POST:url parameters:paramt progress:^(NSProgress * _Nonnull uploadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if ([WSMTools wsmTool].isPost) {
                NSLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
            }else {
                NSLog(@"post:   respj:   %@  msg:%@",responseObject, responseObject[@"msg"]);
            }
        
            if (complete) complete(YES, responseObject, nil);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (complete) complete(YES, nil, error);
            
            NSError *underError = error.userInfo[@"NSUnderlyingError"];
            
            NSData *responseData = underError.userInfo[@"com.alamofire.serialization.response.error.data"];
            
            NSString *result = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];
            
            NSLog(@"result:%@----error:%@",result, error);

            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
        }];
        
//    }
    
    
}


+ (void)uploadImage:(NSString *)url params:(NSMutableDictionary *)params data:(NSData *)data complete:(void (^)(BOOL, id, NSError *))complete{
    
}


+ (void)uploadVideo:(NSString *)url params:(NSMutableDictionary *)params data:(NSData *)data complete:(void (^)(BOOL, id, NSError *))complete{
    
}


+ (void)downloadFileWithURL:(NSString *)url params:(NSMutableDictionary *)params savePath:(NSString *)savePath data:(NSData *)data complete:(void (^)(BOOL, id, NSError *))complete{
    
}

@end
